#include <iostream>
#include<signal.h>
#include "../comm/httplib.h"
#include"oj_control.hpp"

using namespace ns_control;
using namespace httplib;

static Control *ctrl_ptr = nullptr;

void Recovery(int signo)
{
    ctrl_ptr->RecoveryMachine();
}

int main()
{
    signal(SIGQUIT,Recovery);
    // 用户请求服务路由功能
    Server svr;
    Control ctrl;
    ctrl_ptr = &ctrl;
    
    svr.Get("/all_questions", [&ctrl](const Request &req, Response &resp)
            { 
                // 返回一张含有所有题目的网页
                string html;
                ctrl.AllQuestion(&html);
                resp.set_content(html, "text/html;charset=utf-8"); 
            });
    /// question/100 ->正则匹配
    // R"()"不做相关转义
    svr.Get(R"(/question/(\d+))", [&ctrl](const Request &req, Response &resp)//10.47
            {
        std::string num=req.matches[1];
        string html;
        ctrl.Question(num,&html);
        resp.set_content(html,"text/html;charset=utf-8");
         });
    // 用户提交代码, 使用我们的功能(1. 测试用例，2.complie_run)
    svr.Post(R"(/judge/(\d+))", [&ctrl](const Request &req, Response &resp)
        {
        std::string num=req.matches[1];
        string result_json;
        ctrl.Judge(num,req.body,&result_json);
        resp.set_content(result_json,"application/json;charset=utf-8");
         });
    svr.set_base_dir("wwwroot");
    svr.listen("0.0.0.0", 8888);
    return 0;
}